//
//  NSString+Category.m
//  JiaoShiBan
//
//  Created by 暴瑞瑞 on 2017/6/23.
//  Copyright © 2017年 LongShang. All rights reserved.
//

#import "NSString+Category.h"

@implementation NSString (Category)

- (BOOL)isMobile{
    
    NSString *MOBILE = @"^1\\d{10}$";
    NSPredicate *regextestmobile = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", MOBILE];
    return [regextestmobile evaluateWithObject:self];
}

+(NSString *)getDateString:(NSString *)spString
{
    NSDate *confromTimesp = [NSDate dateWithTimeIntervalSince1970:[spString intValue]];
    NSDateFormatter *dateFormat=[[NSDateFormatter alloc]init];
    [dateFormat setDateFormat:@"YYYY-MM-dd"];
    NSString* string=[dateFormat stringFromDate:confromTimesp];
    return string;
}


+(NSString *)getDateStringYMDHM:(NSString *)spString
{
    
  
        NSScanner* scan = [NSScanner scannerWithString:spString];
        int val;
    if ([scan scanInt:&val] && [scan isAtEnd]) {
        
    }else{
        return spString;
    }
    
    NSDate *confromTimesp = [NSDate dateWithTimeIntervalSince1970:[spString intValue]];
    NSDateFormatter *dateFormat=[[NSDateFormatter alloc]init];
    [dateFormat setDateFormat:@"YYYY-MM-dd HH:mm"];
    NSString* string=[dateFormat stringFromDate:confromTimesp];
    return string;
}

+ (NSString *)GetDateString:(NSDate *)date{
    NSDateFormatter * foo = [[NSDateFormatter alloc] init];
    [foo setDateFormat:@"YYYY-MM-dd"];
   NSString * str =  [foo stringFromDate:date];
   NSDate * tempDate = [foo dateFromString:str];
    return [NSString stringWithFormat:@"%.0f",[tempDate timeIntervalSince1970]];
}
- (void)CallPhone:(NSString *)phoneNumbe{
    
    NSMutableString* str=[[NSMutableString alloc] initWithFormat:@"telprompt://%@",phoneNumbe];
   
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:str]];
}

- (CGSize)sizeWithFont:(UIFont *)font maxSize:(CGSize)maxSize
{
    NSDictionary *attrs = @{NSFontAttributeName : font};
    return [self boundingRectWithSize:maxSize options:NSStringDrawingUsesLineFragmentOrigin attributes:attrs context:nil].size;
}



+ (BOOL)stringContainsEmoji:(NSString *)string
{
    __block BOOL returnValue = NO;
    
    [string enumerateSubstringsInRange:NSMakeRange(0, [string length])
                               options:NSStringEnumerationByComposedCharacterSequences
                            usingBlock:^(NSString *substring, NSRange substringRange, NSRange enclosingRange, BOOL *stop) {
                                const unichar hs = [substring characterAtIndex:0];
                                if (0xd800 <= hs && hs <= 0xdbff) {
                                    if (substring.length > 1) {
                                        const unichar ls = [substring characterAtIndex:1];
                                        const int uc = ((hs - 0xd800) * 0x400) + (ls - 0xdc00) + 0x10000;
                                        if (0x1d000 <= uc && uc <= 0x1f77f) {
                                            returnValue = YES;
                                        }
                                    }
                                } else if (substring.length > 1) {
                                    const unichar ls = [substring characterAtIndex:1];
                                    if (ls == 0x20e3) {
                                        returnValue = YES;
                                    }
                                } else {
                                    if (0x2100 <= hs && hs <= 0x27ff) {
                                        returnValue = YES;
                                    } else if (0x2B05 <= hs && hs <= 0x2b07) {
                                        returnValue = YES;
                                    } else if (0x2934 <= hs && hs <= 0x2935) {
                                        returnValue = YES;
                                    } else if (0x3297 <= hs && hs <= 0x3299) {
                                        returnValue = YES;
                                    } else if (hs == 0xa9 || hs == 0xae || hs == 0x303d || hs == 0x3030 || hs == 0x2b55 || hs == 0x2b1c || hs == 0x2b1b || hs == 0x2b50) {
                                        returnValue = YES;
                                    }
                                }
                            }];
    if (returnValue) {
        UIAlertView * aler = [[UIAlertView alloc] initWithTitle:@"不能输入表情符号😁" message:nil delegate:nil cancelButtonTitle:@"确认" otherButtonTitles:nil, nil];
        [aler show];
    }
    
    return returnValue;
}
+ (NSMutableAttributedString * )changeString:(NSString *)ChangeString color:(UIColor *)color font:(UIFont *)font inString:(NSString *)string{
    ChangeString = [NSString stringWithFormat:@"%@",ChangeString];
    string = [NSString stringWithFormat:@"%@",string];
    NSMutableAttributedString * att = [[NSMutableAttributedString alloc] initWithString:string];
    NSRange range = [att.string rangeOfString:ChangeString];
    
    if (color) {
        [att addAttribute:NSForegroundColorAttributeName value:color range:range];
    }
    if (font) {
        [att addAttribute:NSFontAttributeName value:font range:range];
    }
    return att;
    
}


+(void)CallPhone:(NSString *)PhoneNumber{
    NSMutableString* str=[[NSMutableString alloc] initWithFormat:@"telprompt://%@",PhoneNumber];
    // NSLog(@"str======%@",str);
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:str]];
}



@end
